package com.bjqh.edu.UI.Activity.TeacherPersonal;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Class;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.adapter.MyOrderAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class CumulativeClassActivity extends ListActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;

    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(android.R.id.empty)
    LinearLayout empty;
    @BindView(R.id.main)
    FrameLayout main;
    @BindView(android.R.id.list)
    ListView list;
    private MyOrderAdapter adapter;
    private List<Class.RowsBean> rowsBeen;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_cumulative_class);
        ButterKnife.bind(this);
        list.setEmptyView(empty);
        swipeLayout.setColorSchemeResources(R.color.grean);
        swipeLayout.setOnRefreshListener(this);
        initViews();
        initData();
        initEvents();
    }


    protected void initViews() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("火速加载中…");
        dialog.show();
        OkGo.post(AppConstants.BASE_URL + "/room/mdatalist.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        swipeLayout.setRefreshing(false);
                        AppConstants.removeDialog(dialog);
                        Log.e("老师累计上课", s);
                        Gson gson = new Gson();
                        Class aClass = gson.fromJson(s, Class.class);
                        if (aClass.getMsg() != null) {
                            if (aClass.getMsg().equals("timeout")) {
                                startActivity(new Intent(CumulativeClassActivity.this, LoginActivity.class));
                            }
                            if (aClass.getMsg().equals("success")) {
                                rowsBeen = aClass.getRows();
                                //添加适配器
                                adapter = new MyOrderAdapter(CumulativeClassActivity.this, rowsBeen);
                                list.setAdapter(adapter);
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        Toast.makeText(CumulativeClassActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                        Log.e("累计上课列表请求失败失败", e.getMessage() + "");
                        AppConstants.removeDialog(dialog);
                    }
                });

        tvTitle.setText("累计上课");
    }


    protected void initData() {

    }


    protected void initEvents() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onRefresh() {
        initViews();
    }
}
